package com.kzj.laimeimei.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.OrderWebActivity;
import com.kzj.laimeimei.beans.OrderBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.recyclerCard.basic.CardItemView;
import com.kzj.laimeimei.recyclerCard.card.other.OrderCard;
import com.kzj.laimeimei.util.ImageUtil;

/**
 * Created by xyg on 2015/10/26.
 */
public class OrderCardView extends CardItemView<OrderCard> {
    private OrderBean orderBean;
    private Context context;
    private TextView textView_type;
    private TextView textView_status;
    private ImageView imageView_logo;
    private TextView textView_goods_name;
    private TextView textView_goods_amount;
    private TextView textView_coupon;
    private TextView textView_order_amount;
    private TextView textView_prompt;
    private TextView textView_cancel;
    private TextView textView_pay;
    private TextView textView_delete;
    private TextView textView_evaluation;
    private TextView textView_complete;

    public OrderCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public OrderCardView(Context context) {
        super(context);
        this.context = context;
    }

    public OrderCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void build(final OrderCard card) {
        super.build(card);
        orderBean = card.getOrderBean();
        textView_type = (TextView) findViewById(R.id.textView_type);
        textView_status = (TextView) findViewById(R.id.textView_status);
        imageView_logo = (ImageView) findViewById(R.id.imageView_logo);
        textView_goods_name = (TextView) findViewById(R.id.textView_goods_name);
        textView_goods_amount = (TextView) findViewById(R.id.textView_goods_amount);
        textView_coupon = (TextView) findViewById(R.id.textView_coupon);
        textView_order_amount = (TextView) findViewById(R.id.textView_order_amount);
        textView_prompt = (TextView) findViewById(R.id.textView_prompt);
        textView_cancel = (TextView) findViewById(R.id.textView_cancel);
        textView_pay = (TextView) findViewById(R.id.textView_pay);
        textView_delete = (TextView) findViewById(R.id.textView_delete);
        textView_evaluation = (TextView) findViewById(R.id.textView_evaluation);
        textView_complete = (TextView) findViewById(R.id.textView_complete);

        textView_pay.setVisibility(GONE);
        textView_cancel.setVisibility(GONE);
        textView_delete.setVisibility(GONE);
        textView_evaluation.setVisibility(GONE);
        textView_complete.setVisibility(GONE);
        textView_pay.setVisibility(GONE);
        textView_prompt.setVisibility(GONE);
        textView_coupon.setVisibility(GONE);

        App.displayImageHttpOrFile(orderBean.getGoods_image(), imageView_logo, ImageUtil.OptionsNormal());
        textView_status.setText(orderBean.getOrder_status());
        textView_goods_amount.setText(orderBean.getGoods_amount());
        textView_goods_name.setText(orderBean.getGoods_name());
        textView_order_amount.setText(orderBean.getPayment_name() + "：" + orderBean.getOrder_amount());
        if (orderBean.getCoupon_name() != null && !orderBean.getCoupon_name().equals("")) {
            textView_coupon.setVisibility(VISIBLE);
            textView_coupon.setText("优惠劵：" + orderBean.getCoupon_name());
        }
        if (orderBean.getStatus().equals("11")) {
            textView_prompt.setVisibility(VISIBLE);
            textView_pay.setVisibility(VISIBLE);
            textView_cancel.setVisibility(VISIBLE);
            int i = orderBean.getPay_limit().length();
            SpannableStringBuilder spannable = new SpannableStringBuilder("请在" + orderBean.getPay_limit() + "分钟内完成支付，否则该订单将被取消");
            spannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_f875a3_red)), 2, 2 + i + 2,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);//背景色
            textView_prompt.setText(spannable);
        } else if (orderBean.getStatus().equals("20")) {
            textView_cancel.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("25")) {
            textView_complete.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("30")) {
            textView_cancel.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("33")) {
            textView_complete.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("36")) {
            textView_complete.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("40")) {
            if (orderBean.getEvaluation_status().equals("0")) {
                textView_delete.setVisibility(VISIBLE);
                textView_evaluation.setVisibility(VISIBLE);
            } else {
                textView_delete.setVisibility(VISIBLE);
            }
        } else if (orderBean.getStatus().equals("40")) {
            textView_delete.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("55")) {
            textView_delete.setVisibility(VISIBLE);
        } else if (orderBean.getStatus().equals("0")) {
            textView_delete.setVisibility(VISIBLE);
        }
        if (orderBean.getSite_type().equals("1")) {
            textView_type.setText("上门服务");
        } else {
            textView_type.setText("到店服务：" + orderBean.getSeller_name());
        }
        textView_evaluation.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                card.getOnNormalButtonPressedListener().onButtonPressedListener(v, card);
            }
        });
        textView_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                card.getOnNormalButtonPressedListener().onButtonPressedListener(v, card);
            }
        });
        textView_complete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                card.getOnNormalButtonPressedListener().onButtonPressedListener(v, card);
            }
        });
        textView_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                card.getOnNormalButtonPressedListener().onButtonPressedListener(v, card);
            }
        });
        textView_pay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                card.getOnNormalButtonPressedListener().onButtonPressedListener(v, card);
            }
        });

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderWebActivity.class);
                intent.putExtra(OrderWebActivity.WebUrl, orderBean.getDetail_url() + "&user_checked=" + App.getInstance().getUserBean().getUser_checked());
                intent.putExtra("order_bean", orderBean);
                context.startActivity(intent);
            }
        });
    }
}
